package com.xinnet.core.validate;

import java.util.Properties;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import com.xinnet.core.validate.attribute.NonnegativeInteger;


/**
 *验证数字用的Validator类。
 * 
 * @author wenjianye 请输入半角数字。
 */
public class NonnegativeIntegerValidator extends Validator implements
		ConstraintValidator<NonnegativeInteger, Object> {

	private static final String REGEX_PATTERN_DIGIT_POSITIVE_ONLY = "^[0-9]*";
	private static Properties prop;
	@Override
	public void initialize(NonnegativeInteger arg0) {

	}

	/**
	 * 接口的验证方法
	 * */
	@Override
	public boolean isValid(Object value,
			ConstraintValidatorContext constraintContext) {

		return super.isValid(value, constraintContext);
	}

	public boolean valid(Object value) {
		if (value == null) {
			return true;
		}
		if (value instanceof Number) {
			return ((Number) value).longValue() >= 0;
		} else {
			return ValidatorUtil.patternMatches(
					REGEX_PATTERN_DIGIT_POSITIVE_ONLY, value);
		}
	}

	/**
	 * 未通过验证时,提示错误信息
	 * */
	public String getMessage() {
		this.getClass().getCanonicalName();
		return prop
				.getProperty(this.getClass().getName());
	}

}
